<?php

namespace App\Modules\Manage\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class PigProductModel extends Model
{
    protected $table = 'pig_product';
    protected $primaryKey = 'id';

    protected $fillable = [
        'id','cate_id','title','content','cost','stock','is_show','create_time','update_time'
    ];

    public $timestamps = false;
//admin模块
    static function pigProductList()
    {
        $results = self::select(DB::raw('id,title,content,cost,stock,is_show,from_unixtime(create_time) create_time,from_unixtime(update_time) update_time'))->paginate(10);
        return $results;
    }
//admin模块
    static function pigProductInsert(array $param)
    {
        $result = self::create($param);
        return $result;
    }
//admin模块
    static function pigProductGet($id)
    {
        $result = self::where('id',$id)->select(DB::raw('id,cate_id,title,content,cost,stock,is_show'))->first();
        return $result;
    }
//admin模块
    static function pigProductUpdate($param)
    {
        $id = $param['id'];
        unset($param['id']);
        $result = self::where('id',$id)->update($param);
        return $result;
    }
 //home模块
    static function homeIndexProduct($list_num=6)
    {
        $results = self::from('pig_product as pp')->leftJoin('pig_file as pf','pp.id','=','pf.product_id')->where('pp.is_show',1)->select('pp.id','pp.title','pp.content','pp.cost','pf.file_path')->groupBy('pp.id')->limit($list_num)->get();
        return $results;
    }
    //home模块
    static function homeCateProduct($cateId)
    {
        $results = self::from('pig_product as pp')->leftJoin('pig_file as pf','pp.id','=','pf.product_id')->where(['pp.cate_id'=>$cateId,'pp.is_show'=>1])->select('pp.id','pp.title','pp.content','pp.cost','pp.cate_id','pf.file_path')->limit(6)->get();
        return $results;
    }
    //home detail 数据
    static function homeDetailProduct($id)
    {
        $result = self::from('pig_product as pp')->leftJoin('pig_file as pf','pp.id','=','pf.product_id')->where('pp.id',$id)->select('pp.title','pp.content','pp.cost','pf.file_path')->get();
        return $result;
    }
}